package com.deng.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deng.entity.Category;
import com.deng.service.CategoryService;
import com.deng.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.util.List;

@Component
public class ContextStartup implements ApplicationRunner, ServletContextAware {
    //分类列表
    @Autowired
    CategoryService categoryService;
    @Autowired
    PostService postService;
    ServletContext servletContext;
    //在项目启动时调用
    @Override
    public void run(ApplicationArguments args) throws Exception {
        //分类导航栏
        List<Category> categories=categoryService.list(new QueryWrapper<Category>().eq("status",0));//查询status为0的类别
        servletContext.setAttribute("categories",categories);
        //本周热议
        postService.initWeekRank();

    }
    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext=servletContext;
    }
}
